package com.yyy.dm.consumer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyy.dm.CommonResponse;
import com.yyy.dm.VoUtil;
import com.yyy.dm.dao.CinemaSeatMapper;
import com.yyy.dm.item.CinemaSeatDTO;
import com.yyy.dm.item.DMCinemaSeat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CinemaSeatConsumerService {
    @Resource
    private CinemaSeatMapper cinemaSeatMapper;

    public CommonResponse getSeatList(Long cinemaId) {
        QueryWrapper<DMCinemaSeat> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cinemaId", cinemaId);
        List<DMCinemaSeat> dmCinemaSeatList = cinemaSeatMapper.selectList(queryWrapper);
        System.out.println(dmCinemaSeatList);
        Map<Integer, List<DMCinemaSeat>> map = dmCinemaSeatList.stream().collect(Collectors.groupingBy(DMCinemaSeat::getX));
        List<String> stringList = new ArrayList<>();
        for (List<DMCinemaSeat> value : map.values()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (DMCinemaSeat dmCinemaSeat : value) {
                if (dmCinemaSeat.getStatus() == 0) {
                    stringBuilder.append("_");
                } else {
                    if (dmCinemaSeat.getAreaLevel() == 1)
                        stringBuilder.append("a");
                    else if (dmCinemaSeat.getAreaLevel() == 2)
                        stringBuilder.append("b");
                    else
                        stringBuilder.append("c");
                }
            }
            stringList.add(stringBuilder.toString());
        }
        CinemaSeatDTO cinemaSeatDTO = new CinemaSeatDTO();
        cinemaSeatDTO.setCinemaId(cinemaId);
        cinemaSeatDTO.setSeatArray(stringList);
        return VoUtil.returnSuccess(cinemaSeatDTO);
    }
}
